package cn.sdormitory.controller.report;

import cn.sdormitory.common.api.CommonPage;
import cn.sdormitory.common.api.CommonResult;
import cn.sdormitory.report.service.ClassAttenceStaService;
import cn.sdormitory.report.vo.ClassAttenceStaVO;
import com.alibaba.excel.EasyExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @创建人：zhouyang
 * @创建时间：2022/1/17 16:31
 * @version：V1.0
 */
@RestController
@Api(tags = "Report-ClassAttenceStatistics=> 班级考勤统计")
@RequestMapping("/report/classattencestatistics")
public class ClassAttenceStaController {
    @Autowired
    private ClassAttenceStaService classAttenceStaService;

    @ApiOperation("list => 查询正常出勤率统计列表")
    @PreAuthorize("@ss.hasPermi('report:classattencestatistics:list')")
    @GetMapping(value = "/list")
    public CommonResult<CommonPage<ClassAttenceStaVO>> list(@RequestParam Map<String, Object> params) {
        String accDate=(String)params.get("accessDate");
        String accessDate="";
        if("".equals(accDate) || accDate==null){
            Date date = new Date();
            SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
            String date1=fmt.format(date);
            accessDate=date1;
        }else{
            accessDate=accDate;
        }
        String timeSlot=(String)params.get("timeSlot");
        if("".equals(timeSlot) || timeSlot==null){
            timeSlot="1";
        }
        String classDeptId=(String)params.get("classDeptId");
        if("".equals(classDeptId) || classDeptId==null){
            classDeptId="1";
        }

        List<ClassAttenceStaVO> taskTotalVoList = classAttenceStaService.getClassAttenceStaList(accessDate,timeSlot,classDeptId,Integer.parseInt((String)params.get("pageNum")),Integer.parseInt((String)params.get("pageSize")));
        CommonPage<ClassAttenceStaVO> commonPage = new CommonPage<ClassAttenceStaVO>();
        commonPage.setList(taskTotalVoList);
        commonPage.setTotal(classAttenceStaService.getTotalCountByDate(accessDate,timeSlot,classDeptId));
        return CommonResult.success(commonPage);

    }


    @ApiOperation("hygienelist =>导出班级考勤统计信息")
    @PreAuthorize("@ss.hasPermi('report:exportclassattencesta:attencelist')")
    @GetMapping(value = "/exportclassattencelist")
    public CommonResult export(HttpServletResponse response, @RequestParam Map<String, Object> params) {
        String accDate=(String)params.get("accessDate");
        String accessDate="";
        if("".equals(accDate) || accDate==null){
            Date date = new Date();
            SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
            String date1=fmt.format(date);
            accessDate=date1;
        }else{
            accessDate=accDate;
        }
        String timeSlot=(String)params.get("timeSlot");
        if("".equals(timeSlot) || timeSlot==null){
            timeSlot="1";
        }
        String classDeptId=(String)params.get("classDeptId");
        if("".equals(classDeptId) || classDeptId==null){
            classDeptId="1";
        }

        List<ClassAttenceStaVO> taskTotalVoList = classAttenceStaService.getClassAttenceStaListExport(accessDate,timeSlot,classDeptId);
        try {
            EasyExcel.write(response.getOutputStream(), ClassAttenceStaVO.class).sheet("班级考勤统计管理").doWrite(taskTotalVoList);
            return CommonResult.success();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return CommonResult.failed();

    }
}
